// page_two/drugcc/drugcc.js
let app = getApp()
let yuming = app.globalData.yuming
let fuhao = app.globalData.fuhao
var shuzong = []
var shu = []
const request = require('../../config/request');
Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    yuming: yuming,
    heights: "",
    title: '',
    buttonClicked: false,
    huan: ['药品品种', '监测设备', '温湿度监测'],
    currtab: 0,
    gongwu: false,
    // sbList: [{
    //   createtime: '2021-08-25',
    //   sbname: '设备',
    //   sbbh: 'AC1243',
    //   scph: '210914-1',
    //   sccs: 'xxx厂',
    //   sbhgz: `${yuming}static/applet/images/xiaofei/images/linshi/wem.jpg`,
    //   sblz: `${yuming}static/applet/images/xiaofei/images/linshi/wyn.jpg`
    // }],
    sbList: [],
    sbname: '',
    sbbh: '',
    scph: '',
    sccs: '',
    sbhgz: '',
    wusbhgz: '',
    sblz: '',
    wusblz: '',
    page: 1,
    yiyao: ''
  },
  tianjia() {
    wx.navigateTo({
      url: 'url',
    })
  },
  qies(e) {
    shuzong = []
    shu = []
    var index = e.currentTarget.dataset.currtab
    this.setData({
      currtab: index,
      page: 1,
      sbname: '',
      sbbh: '',
      scph: '',
      sccs: '',
      sbhgz: '',
      wusbhgz: '',
      sblz: '',
      wusblz: '',
      gongwu: false,
    })
  },
  // 冷链
  lenglian() {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    let that = this
    this.myRequest(yuming + 'Shop/shop_checkout_alllist', {
      shopId: wx.getStorageSync('shopId'),
      page: that.data.page,
      satrtime: '',
      endtime: ''
    }, "POST").then(res => {
      if (res.data.code == 200) {
        wx.hideLoading();
        shu = res.data.data
        if (that.data.page != 1) {
          if (shu.length != 10) {
            that.setData({
              gongwu: true
            })
          }
        }
        that.setData({
          sbList: shuzong.concat(shu)
        })
      }
    })
  },
  // 存储设备列表接口
  getccsbList() {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    let that = this
    this.myRequest(yuming + 'Yiyao/yiyaoDeviceList', {
      shopId: wx.getStorageSync('shopId'),
      page: that.data.page
    }, "POST").then(res => {
      if (res.data.code == 200) {
        wx.hideLoading();
        shu = res.data.data
        if (that.data.page != 1) {
          if (shu.length != 10) {
            that.setData({
              gongwu: true
            })
          }
        }
        that.setData({
          sbList: shuzong.concat(shu)
        })
      }
    })
  },
  // 查看更多
  lookmore() {
    shuzong = this.data.sbList
    this.setData({
      page: (this.data.page) + 1
    })
    this.getccsbList();
  },
  // 设备名称
  sbname(e) {
    this.setData({
      sbname: e.detail.value
    })
  },
  // 设备编号
  sbbh(e) {
    this.setData({
      sbbh: e.detail.value
    })
  },
  // 生产批号
  scph(e) {
    this.setData({
      scph: e.detail.value
    })
  },
  // 生产厂商
  sccs(e) {
    this.setData({
      sccs: e.detail.value
    })
  },
  // 上传
  chuan(e) {
    var aaa = e.currentTarget.dataset.aaa
    var that = this;
    //选取图片
    this.myChooseImage(1).then(res => {
      const tempFilePaths = res.tempFilePaths
      this.myUploadImg(tempFilePaths[0], 'file').then(res => {
        var data = JSON.parse(res.data)
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
        var urls = data.httpUrl + data.imgUrl
        //设备合格证
        if (aaa == 1) {
          that.setData({
            sbhgz: urls,
            wusbhgz: data.imgUrl
          })
        }
        // 设备留证
        if (aaa == 2) {
          that.setData({
            sblz: urls,
            wusblz: data.imgUrl
          })
        }
      })
    })
  },
  // 点击放大
  fangdas(e) {
    var img = e.currentTarget.dataset.img
    wx.previewImage({
      urls: [img]
    })
  },
  // 验证
  ti() {
    if (this.data.sbname == '') {
      wx.showToast({
        title: '请输入设备名称',
        icon: 'none'
      })
    } else if ((fuhao.test(this.data.sbname))) {
      wx.showToast({
        title: '设备名称格式错误',
        icon: 'none'
      })
      this.setData({
        sbname: ''
      })
    } else if (this.data.sbbh == '') {
      wx.showToast({
        title: '请输入设备编号',
        icon: 'none'
      })
    } else if ((fuhao.test(this.data.sbbh))) {
      wx.showToast({
        title: '设备编号格式错误',
        icon: 'none'
      })
      this.setData({
        sbbh: ''
      })
    } else if (this.data.scph == '') {
      wx.showToast({
        title: '请输入生产批号',
        icon: 'none'
      })
    } else if ((fuhao.test(this.data.scph))) {
      wx.showToast({
        title: '生产批号格式错误',
        icon: 'none'
      })
      this.setData({
        scph: ''
      })
    } else if (this.data.sccs == '') {
      wx.showToast({
        title: '请输入生产厂商',
        icon: 'none'
      })
    } else if ((fuhao.test(this.data.sccs))) {
      wx.showToast({
        title: '生产厂商格式错误',
        icon: 'none'
      })
      this.setData({
        sccs: ''
      })
    } else if (this.data.wusbhgz == '') {
      wx.showToast({
        title: '请上传设备合格证',
        icon: 'none'
      })
    } else if (this.data.wusblz == '') {
      wx.showToast({
        title: '请上传设备留证',
        icon: 'none'
      })
    } else {
      this.que()
    }
  },
  // 添加设备
  que() {
    let that = this
    that.setData({
      buttonClicked: true
    })
    this.myRequest(yuming + 'Yiyao/yiyaoDeviceAdd', {
      shopId: wx.getStorageSync('shopId'),
      name: that.data.sbname,
      devcode: that.data.sbbh,
      pihao: that.data.scph,
      changshang: that.data.sccs,
      hege_img: that.data.wusbhgz,
      liuzheng_img: that.data.wusblz,
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        that.getccsbList();
      } else {
        that.setData({
          buttonClicked: false
        })
      }

    })
    setTimeout(function () {
      that.setData({
        currtab: 0,
        buttonClicked: false,
        sbname: '',
        sbbh: '',
        scph: '',
        sccs: '',
        sbhgz: '',
        wusbhgz: '',
        sblz: '',
        wusblz: '',
      })
    }, 1000)
  },
  // 删除
  shanchu(e) {
    var id = e.currentTarget.dataset.id
    var that = this
    wx.showModal({
      title: '您确定要删除吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: function (res) {
        if (res.confirm) {
          console.log("确定")
          that.dele(id)
        } else {
          console.log("取消")
        }
      }
    })
  },
  // 删除接口
  dele(id) {
    let that = this
    this.myRequest(yuming + 'Yiyao/yiyaoDeviceDel', {
      id: id
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        shuzong = [];
        shu = [];
        that.getccsbList();
      }
    })
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      shopType: options.shopType,
      title: options.title
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      yiyao: wx.getStorageSync('stryId')
    })
    // if (this.data.yiyao != 6) {
    this.getccsbList();
    // } else {
    //   this.lenglian();
    // }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})